////////////////////////////////////////////////////////////////////////////////////////
//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING THIS SOFTWARE
//
//  By downloading, copying, installing or using the software you agree to this license.
//  If you do not agree to this license, do not download, install, copy or use the software.
//
// Copyright (C) 2013, AerVision Technologies Pty Ltd, all rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
//   * Redistribution's of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//
//   * Redistribution's in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
//     and/or other materials provided with the distribution.
//
//   * The name of AerVision Technologies, may not be used to endorse or promote products
//     derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors to demonstrate 
// www.AerCloud.net Framework as a Service API (FaaS), "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the copyright holder or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
////////////////////////////////////////////////////////////////////////////////////////

package com.aercloud.client.gui;

import java.awt.Cursor;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import org.apache.log4j.Logger;

import com.aercloud.client.util.AerCloudClient;

public class PanelStep2 extends JPanel {
    
    private static Logger log = Logger.getLogger(PanelStep2.class);
    
    private File dbDir;
    private BufferedImage currentImage;
    private boolean saveInProgress;
    
    private JLabel lblImage;
    private JLabel lblImageFace;
    private PanelStep2List panelList;
    
    private JFrame frame;

    /**
     * Create the panel.
     */
    public PanelStep2() {
        GridBagLayout gridBagLayout = new GridBagLayout();
        gridBagLayout.columnWidths = new int[]{0, 0, 0, 0, 0, 0};
        gridBagLayout.rowHeights = new int[]{0};
        gridBagLayout.columnWeights = new double[]{1.0, 1.0, 1.0, 1.0, 1.0, 1.0};
        gridBagLayout.rowWeights = new double[]{1.0};
        setLayout(gridBagLayout);
        GridBagConstraints gbc_panelList = new GridBagConstraints();
        gbc_panelList.fill = GridBagConstraints.BOTH;
        gbc_panelList.gridx = 0;
        gbc_panelList.gridy = 0;
        add(getPanelList(), gbc_panelList);
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                try {
                    dbDir = new File(Thread.currentThread().getContextClassLoader().getResource("DB").toURI());
                    log.debug("Successfully located " + dbDir.getPath());
                    if(dbDir.exists() && dbDir.isDirectory() && dbDir.canRead()) {
                        for(String str : dbDir.list(new FilenameFilterImage())) {
                            getPanelList().getListModel().addElement(str);
                        }
//
// The following shit overrides any ListModel previously set
//                    getPanelList().getList().setListData(dbDir.list(new FilenameFilterImage()));
                    }
                    PanelStep2.this.getPanelList().getBtnSave().getAction().setEnabled(!getPanelList().getListModel().isEmpty());
                } catch(Exception e) {
                    log.debug("Failed to locate DB directory", e);
                }
            }
        });
        getPanelList().getList().addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                if(!e.getValueIsAdjusting()) {
                    SwingUtilities.invokeLater(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                PanelStep2.this.getLblImageFace().setIcon(null);
                                PanelStep2.this.currentImage = 
                                    ImageIO.read(new File(PanelStep2.this.dbDir, (String)PanelStep2.this.getPanelList().getList().getSelectedValue()));
                                PanelStep2.this.getLblImage().setIcon(
                                    new ImageIcon(PanelStep2.this.currentImage));
                            } catch (Exception e) {
                                PanelStep2.this.currentImage = null;
                                PanelStep2.this.getLblImage().setIcon(null);
                                log.info("Failed to load selected image.");
                                log.debug("Failed to load selected image.", e);
                            }
                        }
                    });
                }
            }
        });
        GridBagConstraints gbc_scrollPaneImage = new GridBagConstraints();
        gbc_scrollPaneImage.gridwidth = 4;
        gbc_scrollPaneImage.fill = GridBagConstraints.BOTH;
        gbc_scrollPaneImage.gridx = 1;
        gbc_scrollPaneImage.gridy = 0;
        add(getLblImage(), gbc_scrollPaneImage);
        GridBagConstraints gbc_scrollPaneImageFace = new GridBagConstraints();
        gbc_scrollPaneImageFace.fill = GridBagConstraints.BOTH;
        gbc_scrollPaneImageFace.gridx = 5;
        gbc_scrollPaneImageFace.gridy = 0;
        add(getLblImageFace(), gbc_scrollPaneImageFace);
    }
    
    public void setFrame(JFrame frame) {
        this.frame = frame;
    }
    
    public void saveFace(final AerCloudClient aerCloudClient, final String accessKey) {
        if(currentImage != null && !saveInProgress) {
            aerCloudClient.getExecPool().submit(new Runnable() {
                @Override
                public void run() {
                    saveInProgress = true;
                    PanelStep2.this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
                    BufferedImage faceImage =
                        aerCloudClient.saveImage(accessKey, (String)getPanelList().getList().getSelectedValue(), currentImage);
                    if(faceImage != null) {
                        getLblImageFace().setIcon(new ImageIcon(faceImage));
                    }
                    PanelStep2.this.getPanelList().removeSelected();
                    PanelStep2.this.setCursor(Cursor.getDefaultCursor());
                    saveInProgress = false;
                }
            });
        }
    }
    
    @Override
    public void setCursor(Cursor cursor) {
        if(frame != null) {
            frame.setCursor(cursor);
        } else {
            super.setCursor(cursor);
        }
    }
    
    public JLabel getLblImage() {
        if (lblImage == null) {
        	lblImage = new LabelImage();
        	lblImage.setVerticalAlignment(SwingConstants.TOP);
        }
        return lblImage;
    }
    public JLabel getLblImageFace() {
        if (lblImageFace == null) {
        	lblImageFace = new LabelImage();
        	lblImageFace.setVerticalAlignment(SwingConstants.BOTTOM);
        }
        return lblImageFace;
    }
    public PanelStep2List getPanelList() {
        if (panelList == null) {
        	panelList = new PanelStep2List();
        }
        return panelList;
    }
}
